//
//  RHChoicePolicyModeView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/3/12.
//

#import "RHChoicePolicyModeView.h"
@interface RHChoicePolicyModeView()
@property (nonatomic ,strong) UIView *contentView;

@property (nonatomic ,strong) MASConstraint *bgViewTopConstraint;

@property (nonatomic ,strong) MASConstraint *bgViewBottomConstraint;

@property (nonatomic ,strong) UIScrollView *scrollView;

@property (nonatomic ,strong) UIView *scrollContentView;

@property (nonatomic ,strong) QMUIButton *lastButton;
@end
@implementation RHChoicePolicyModeView

-(instancetype)init
{
    if(self = [super init]){
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0]];
    
    UIView *touchView = [[UIView alloc] init];
    [touchView setBackgroundColor:UIColor.clearColor];
    [self addSubview:touchView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    touchView.userInteractionEnabled = YES;
    [touchView addGestureRecognizer:tap];
    
    UIView *contentView = kView;
    contentView.backgroundColor = UIColor.whiteColor;
    [self addSubview:contentView];
    self.contentView = contentView;
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        self.bgViewTopConstraint = make.top.equalTo(self.mas_bottom);
       
    }];
    
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setText:@"模式选择".rh_localized];
    [_titleLab setTextAlignment:NSTextAlignmentCenter];
    [_titleLab setFont:[UIFont systemFontOfSize:16]];
    [_titleLab setTextColor:FontColor(@"#969799")];
    _titleLab.backgroundColor = UIColor.whiteColor;
    [contentView addSubview:_titleLab];
    
    QMUIButton *closeBtn = [[QMUIButton alloc]init];
    [closeBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    [contentView addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(40);
        make.right.mas_offset(-10);
        make.centerY.equalTo(weakSelf.titleLab);
    }];

    
    _scrollView = [[UIScrollView alloc]init];
    [contentView addSubview:_scrollView];
    _scrollContentView = kView;
    [_scrollView addSubview:_scrollContentView];
    
    UIView *lineV = [[UIView alloc] init];
    [lineV setBackgroundColor:FontColor(@"#ffffff")];
    [contentView addSubview:lineV];
    
    UIView *btnView = kView;
    btnView.backgroundColor = UIColor.whiteColor;
    [contentView addSubview:btnView];
    [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
    }];
    
    QMUIButton *cancelBtn = [[QMUIButton alloc]init];
    [cancelBtn setTitle:@"取消".rh_localized forState:UIControlStateNormal];
    [cancelBtn setTitleColor:kCOLOR_HEX(@"CCCCCC") forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = kSYSTEMFONT(16);
    cancelBtn.layer.borderColor = kCOLOR_HEX(@"CCCCCC").CGColor;
    cancelBtn.layer.borderWidth = 1;
    cancelBtn.layer.cornerRadius = 8;
    [btnView addSubview:cancelBtn];
    CGFloat bottom_margin = 0;
    if(kSafe_bottom == 0){
        bottom_margin = 10;
    }
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(0);
        make.left.mas_offset(25);
        make.height.offset(kScaleW(43));
        make.bottom.offset(-bottom_margin-kSafe_bottom);
    }];
    
    QMUIButton *sureBtn = [[QMUIButton alloc]init];
    [sureBtn setTitle:@"确定".rh_localized forState:UIControlStateNormal];
    sureBtn.titleLabel.font = kSYSTEMFONT(15);
    [sureBtn setTitleColor:FontColor(@"#ffffff") forState:UIControlStateNormal];
    sureBtn.layer.cornerRadius = 8;
    [sureBtn setGradientBackgroundWithColors:@[kCOLOR_HEX(@"#405DBE"),kCOLOR_HEX(@"#405DBE")] locations:@[@(0),@(1)] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5)];
    [contentView addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cancelBtn.mas_right).offset(35);
        make.width.equalTo(cancelBtn);
        make.centerY.equalTo(cancelBtn);
        make.right.mas_offset(-25);
        make.height.offset(45);
    }];
    
    UIView *marginView = kView;
    marginView.backgroundColor = UIColor.whiteColor;
    [contentView addSubview:marginView];
    [marginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.height.offset(0);
        make.bottom.equalTo(btnView.mas_top);
    }];
    
    
    [touchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
        make.height.mas_equalTo(49);
        make.top.mas_offset(0);
    }];
    
    
    
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(1);
        make.top.equalTo(weakSelf.titleLab.mas_bottom);
    }];
    

    
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.titleLab.mas_bottom);
        make.left.right.mas_offset(0);
        make.bottom.equalTo(marginView.mas_top).offset(-10);
        make.height.offset(45*6);
    }];
    [_scrollContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
        make.width.equalTo(weakSelf.scrollView);
    }];
    
    contentView.layer.cornerRadius = 8;
    contentView.layer.maskedCorners = kCALayerMinXMinYCorner|kCALayerMaxXMinYCorner;
    
    [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        
        [weakSelf hidden];
    }];
    
    [[closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf hidden];
    }];
    
    [[cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf hidden];
    }];
    
    [[sureBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
     
        if (weakSelf.choiceResultBlock) {
            weakSelf.choiceResultBlock(weakSelf.curMode);
        }
        [weakSelf hidden];
        
    }];

}

-(void)setPolicyList:(NSArray *)policyList
{
    _policyList = policyList;
    [self updateUI];
}


- (void)setCurMode:(NSInteger )curMode{
    _curMode = curMode;
   
}

-(void)updateUI{
    kWeakSelf;
    [_scrollContentView qmui_removeAllSubviews];
    __block QMUIButton *lastBtn;
    [self.policyList enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        QMUIButton *btn = [[QMUIButton alloc]init];
        [btn setTitle:obj.rh_localized forState:UIControlStateNormal];
        btn.titleLabel.font = kSYSTEMFONT(14);
        [btn setTitleColor:[UIColor colorWithHexString:@"3F4443" alpha:0.8] forState:UIControlStateNormal];
        btn.backgroundColor = kCOLOR_HEX(@"F6F8FA");
        btn.layer.cornerRadius = 8;
        btn.userInteractionEnabled = YES;
        [weakSelf.scrollContentView addSubview:btn];
        
        
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset((kUI_WINDOW_W-45)/2);
            make.height.offset(kScaleW(45));
            if(!lastBtn){
                make.left.top.mas_offset(15);
            }else{
                if(idx%2 == 0){
                    make.left.mas_offset(15);
                    make.top.equalTo(lastBtn.mas_bottom).offset(15);
                }else{
                    make.left.equalTo(lastBtn.mas_right).offset(15);
                    make.top.equalTo(lastBtn);
                }
                
            }
        }];
        lastBtn = btn;
        
        [[btn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
            RHPolicyModeModel *model = weakSelf.policyList[idx];
            
            weakSelf.curMode = idx;
            
//            if(![weakSelf.lastButton isEqual:x]){
//               
//                [x setTitleColor:[UIColor colorWithHexString:@"ffffff" alpha:1] forState:UIControlStateNormal];
//                [x setGradientBackgroundWithColors:@[kCOLOR_HEX(@"6AD785"),kCOLOR_HEX(@"5ECBB7")] locations:@[@(0),@(1)] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5)];
//                
//                [weakSelf.lastButton setTitleColor:[UIColor colorWithHexString:@"3F4443" alpha:0.6] forState:UIControlStateNormal];
//                [weakSelf.lastButton setGradientBackgroundWithColors:@[kCOLOR_HEX(@"F6F8FA"),kCOLOR_HEX(@"F6F8FA")] locations:@[@(0),@(1)] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5)];
//                
//                weakSelf.lastButton = x;
//            }
        }];
        
        [RACObserve(self, curMode) subscribeNext:^(id  _Nullable x) {
            if([x integerValue] == idx){
                [btn setTitleColor:[UIColor colorWithHexString:@"ffffff" alpha:1] forState:UIControlStateNormal];
                [btn setGradientBackgroundWithColors:@[kCOLOR_HEX(@"#405DBE"),kCOLOR_HEX(@"#405DBE")] locations:@[@(0),@(1)] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5)];
            }else{
                [btn setTitleColor:[UIColor colorWithHexString:@"3F4443" alpha:0.6] forState:UIControlStateNormal];
                [btn setGradientBackgroundWithColors:@[kCOLOR_HEX(@"F6F8FA"),kCOLOR_HEX(@"F6F8FA")] locations:@[@(0),@(1)] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5)];
            }
        }];
//        if([weakSelf.curMode.modeName isEqualToString:obj.modeName]){
//            [btn setTitleColor:[UIColor colorWithHexString:@"ffffff" alpha:1] forState:UIControlStateNormal];
//            [btn setGradientBackgroundWithColors:@[kCOLOR_HEX(@"6AD785"),kCOLOR_HEX(@"5ECBB7")] locations:@[@(0),@(1)] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5)];
//            weakSelf.lastButton = btn;
//        }
        
    }];
    [lastBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(0);
    }];
}

- (void)hidden{
    kWeakSelf;
    [self.bgViewBottomConstraint uninstall];
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        self.bgViewTopConstraint = make.top.equalTo(self.mas_bottom);

    }];
    [UIView animateWithDuration:0.2 animations:^{
        [self setNeedsLayout];
        [self layoutIfNeeded];
        [self setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0]];
    } completion:^(BOOL finished) {
      
        [self removeFromSuperview];
       
    }];
}


- (void)showAnimation{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.frame = [UIApplication sharedApplication].keyWindow.bounds;
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.bgViewTopConstraint uninstall];
            [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
                self.bgViewBottomConstraint = make.bottom.mas_offset(0);

            }];
            [UIView animateWithDuration:0.2 animations:^{
                [self setNeedsLayout];
                [self layoutIfNeeded];
                [self setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0.3]];
            }];
    });

}



- (void)hiddenAnimation{
    
    
}




-(void)dealloc{
    NSLog(@"---->ChoicePolicyView dealloc");
}

@end
